package com.ruoyi.system.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.dto.ShipmentOrderDetailDTO;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.vo.ShipmentOrderDetailVO;
import com.ruoyi.system.vo.ShipmentOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IShipmentOrderDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库单详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class ShipmentOrderDetailServiceImpl implements IShipmentOrderDetailService
{
    @Autowired
    private ShipmentOrderDetailMapper shipmentOrderDetailMapper;

    @Autowired
    private ShipmentOrderMapper shipmentOrderMapper;

    @Autowired
    private WarehouseMapper wmsWarehouseMapper;

    @Autowired
    private WmsAreaMapper wmsAreaMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private HistoryMapper inventoryHistoryMapper;

    /**
     * 查询出库单详情
     * 
     * @param id 出库单详情主键
     * @return 出库单详情
     */
//    @Override
//    public ShipmentOrderDetail selectShipmentOrderDetailById(Long id)
//    {
//        return shipmentOrderDetailMapper.selectShipmentOrderDetailById(id);
//    }

    /**
     * 查询出库单详情列表
     * 
     * @param shipmentOrderDetail 出库单详情
     * @return 出库单详情
     */
    @Override
    public List<ShipmentOrderDetailDTO> selectShipmentOrderDetailList(ShipmentOrderDetail shipmentOrderDetail)
    {
        return shipmentOrderDetailMapper.selectShipmentOrderDetailList(shipmentOrderDetail);
    }

    @Override
    public List<Map<String, Object>> selectShipmentOrderList(ShipmentOrderDetail shipmentOrderDetail) {
        return shipmentOrderDetailMapper.selectShipmentOrderList(shipmentOrderDetail);
    }

    /**
     * 新增出库单详情
     * 
     * @param shipmentOrderDetail 出库单详情
     * @return 结果
     */
    @Override
    public int insertShipmentOrderDetail(ShipmentOrderDetail shipmentOrderDetail)
    {
        shipmentOrderDetail.setCreateTime(DateUtils.getNowDate());
        return shipmentOrderDetailMapper.insertShipmentOrderDetail(shipmentOrderDetail);
    }

    /**
     * 修改出库单详情
     * 
     * @param shipmentOrderDetail 出库单详情
     * @return 结果
     */
    @Override
    public int updateShipmentOrderDetail(ShipmentOrderDetail shipmentOrderDetail)
    {
        shipmentOrderDetail.setUpdateTime(DateUtils.getNowDate());
        return shipmentOrderDetailMapper.updateShipmentOrderDetail(shipmentOrderDetail);
    }

    /**
     * 批量删除出库单详情
     * 
     * @param ids 需要删除的出库单详情主键
     * @return 结果
     */
    @Override
    public int deleteShipmentOrderDetailByIds(Long[] ids)
    {
        return shipmentOrderDetailMapper.deleteShipmentOrderDetailByIds(ids);
    }

    /**
     * 删除出库单详情信息
     * 
     * @param id 出库单详情主键
     * @return 结果
     */
    @Override
    public int deleteShipmentOrderDetailById(Long id)
    {
        return shipmentOrderDetailMapper.deleteShipmentOrderDetailById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateShipmentOrderDetailState(ShipmentOrderDetailVO shipmentOrderDetail) throws SQLException {
        //将入库单拆分转换为Long类型
        shipmentOrderDetail.setShipmentOrderId(Long.parseLong(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2)));
        shipmentOrderDetailMapper.updateShipmentOrderDetailState(shipmentOrderDetail);

        //只有状态为待发货才能进行发货，否则直接修改状态
        if(shipmentOrderDetail.getShipmentOrderStatus() == 3){
            //实例化一个库存对象接收参数去查询仓库内该物料是否有充足
            Inventory inventory = new Inventory();
            inventory.setItemId(shipmentOrderDetail.getItemId());
            inventory.setWarehouseId(shipmentOrderDetail.getWarehouseId());
            inventory.setAreaId(shipmentOrderDetail.getAreaId());
            Warehouse wmsWarehouse = wmsWarehouseMapper.selectWarehouseById(shipmentOrderDetail.getWarehouseId());
            WmsArea wmsArea = wmsAreaMapper.selectWmsAreaById(shipmentOrderDetail.getAreaId());
            Inventory inv = inventoryMapper.selectInventoryByItemId(inventory);
            if(inv.getQuantity().compareTo(shipmentOrderDetail.getPlanQuantity()) == 1) {
                //修改库存记录
                inv.setQuantity(inv.getQuantity().subtract(shipmentOrderDetail.getPlanQuantity()));
                inventoryMapper.updateInventory(inv);
                //创建库存记录信息并添加进数据库
                History inventoryHistory = new History();
                inventoryHistory.setFormId(Long.parseLong(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2)));
                inventoryHistory.setFormType(2L);
                inventoryHistory.setItemId(shipmentOrderDetail.getItemId());
                inventoryHistory.setQuantity(shipmentOrderDetail.getPlanQuantity().negate());
                inventoryHistory.setCreateTime(DateUtils.getNowDate());
                inventoryHistory.setWarehouseId(shipmentOrderDetail.getWarehouseId());
                inventoryHistory.setAreaId(shipmentOrderDetail.getAreaId());
                inventoryHistoryMapper.insertHistory(inventoryHistory);

                //查询判断出库的数量
                List<ShipmentOrderDetail> query = shipmentOrderDetailMapper.query(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2));
                List<ShipmentOrderDetail> shipmentOrderDetails = shipmentOrderDetailMapper.selectNubmer(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2));

                //实例化一个入库单对象 传入状态 进行修改
                ShipmentOrderVO shipmentOrder = new ShipmentOrderVO();
                shipmentOrder.setShipmentOrderNo(shipmentOrderDetail.getShipmentOrderNo());
                if(query.size() == shipmentOrderDetails.size()){
                    shipmentOrder.setShipmentOrderStatus(3L);
                }else{
                    shipmentOrder.setShipmentOrderStatus(2L);
                }
                shipmentOrderMapper.updateShipmentOrderByShipmentOrderNo(shipmentOrder);

                //实例化发货对象

                return AjaxResult.success();
            }else{
                throw new SQLException(wmsWarehouse.getWarehouseName()+"库内"+wmsArea.getAreaName()+"的"+shipmentOrderDetail.getItemName()+"物料库存不足");
            }
        }else{
            //查询判断出库的数量
            List<ShipmentOrderDetail> query = shipmentOrderDetailMapper.query(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2));
            List<ShipmentOrderDetail> shipmentOrderDetails = shipmentOrderDetailMapper.selectNubmer(shipmentOrderDetail.getShipmentOrderNo().replace("-", "").substring(2));

            //判断出库数量
            if(query.size() == shipmentOrderDetails.size()){
                //实例化一个入库单对象 传入状态 进行修改
                ShipmentOrderVO shipmentOrder = new ShipmentOrderVO();
                shipmentOrder.setShipmentOrderNo(shipmentOrderDetail.getShipmentOrderNo());
                shipmentOrder.setShipmentOrderStatus(3L);
                shipmentOrderMapper.updateShipmentOrderByShipmentOrderNo(shipmentOrder);
            }
            return AjaxResult.success();
        }
    }

    @Override
    public int selectStatussChuku(int status) {
        return shipmentOrderDetailMapper.selectStatussChuku(status);
    }
}
